import { createRouter, createWebHistory } from 'vue-router'
// import HomeView from '@/components/HomeView'
import Layout from '@/views/Layout'
import Home from '@/views/Home'
import Openvpn from '@/views/VpnList'
import Login from '@/views/Login'
import ServerList from '@/views/ServerList'
import Record from '@/views/Record'
import SoftwareImage from '@/views/SoftwareImage'

const routes = [
  {
    path: '/',
    redirect: { path: '/login' }
  },
  {
    path: '/login',
    name: 'login',
    meta: {
      title: '登录',
      requiresAuth: false // false表示不需要登录
    },
    component: Login
  },
  { path: '/401',
    name: '401', component: () => import('@/views/Common/401.vue'),
    meta: { title_cn: '401', title_en: '401'
  }
  },
  { path: '/404',
    name: '404', component: () => import('@/views/Common/404.vue'),
    meta:
      { title_cn: '404', title_en: '404' }
  },
  { path: '/500',
    name: '500', component: () => import('@/views/Common/500.vue'),
    meta: { title_cn: '500', title_en: '500' }
  },

  // {
  //   path: '/home',
  //   name: 'home',
  //   meta: {
  //     title: '首页',
  //     requiresAuth: true // true表示需要登录
  //   },
  //   component: HomeView
  // },
  {
    path: '/',
    name: 'layout',
    redirect: '/home',
    component: Layout,
    children: [
      {
        path: '/home',
        name: 'home',
        component: Home,
        meta: {
          title: '首页',
          icon: 'HomeFilled',
          requiresAuth: true // true表示需要登录
        }
      },
      {
        path: '/vpnuser-list',
        name: 'VpnList',
        component: Openvpn,
        meta: {
          title: 'VPN用户管理',
          icon: "Connection",
          requiresAuth: true
        }
      },
      {
        path: '/server-list',
        name: "ServerList",
        component: ServerList,
        meta: {
          title: 'GDC服务器列表',
          icon: "Grid",
          requiresAuth: true
        }
      },
      {
        path: '/record',
        name: 'record',
        component: Record,
        meta: {
          title: '运维事件记录',
          icon: "Edit"
        }
      },
      {
        path: '/softwareImage',
        name: 'softwareImage',
        component: SoftwareImage,
        meta: {
          title: '常用软件镜像站',
          icon: "Position"
        }
      }
    ]
  }
  // // 上面的是一种写法，可以放在children下面，还有一种方法是下面的这种
  // {
  //   path: '/member',
  //   component: Layout,
  //   children: [
  //     {
  //       path: '', // 等价于 /member/,请求 /member 时会在后面拼接个 / 因为AppNavbar下的index.vue写的是/member/
  //       component: Member,
  //       meta: { title: 'VPN用户' }
  //     }
  //   ]
  // }
]
const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

// // 挂载路由导航守卫
// 路由拦截，判断是否需要登录
router.beforeEach((to, from, next) => {
  if (to.meta.title) {
    // 判断是否有标题
    document.title = to.meta.title
  }
  // 通过requiresAuth判断当前路由导航是否需要登录
  if (to.matched.some(record => record.meta.requiresAuth)) {
    const token = localStorage.getItem('token')
    // console.log("token",token)
    // 若需要登录访问，检查是否为登录状态
    if (!token) {
      next({
        path: '/login',
        query: { redirect: to.fullPath }
      })
    } else {
      next()
    }
  } else {
    next() // 确保一定要调用 next()
  }
})

export default router
